package com.xiaoq.shop.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.interpreter.Inter4SPUVo;
import com.xiaoq.apis.order.ISPUApi;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.entity.SPUEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenjh on 2017/11/30.
 */
@Service(interfaceClass=ISPUApi.class, version="1.0.0")
public class SPUApiImpl implements ISPUApi {

    private static final Logger LOGGER = LoggerFactory.getLogger(SPUApiImpl.class);

    @Autowired
    private ISPUService spuService;

    @Autowired
    private ISKUService skuService;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public boolean save(Inter4SPUVo inter4SPUVo) {
        if(inter4SPUVo==null || StringUtils.isAnyBlank(inter4SPUVo.getInterpreterId(),inter4SPUVo.getShopId())){
            return false;
        }
        String spuCode = idGenerator.nextId();
        String spuCatalogCode = idGenerator.nextId();
        String spuId = idGenerator.nextId();
        String skuId = idGenerator.nextId();
        String skuCode = idGenerator.nextId();

        SPUEntity newSpuEntity = new SPUEntity();
        newSpuEntity.setId(spuId);
        newSpuEntity.setCatalogCode("1"); //讲解员
        newSpuEntity.setCatalogName("讲解员");//讲解员
        newSpuEntity.setSaleState(1);
        newSpuEntity.setShopId(inter4SPUVo.getShopId());
        newSpuEntity.setSpuCatalogCode(spuCatalogCode);
        newSpuEntity.setSpuCode(spuCode);
        newSpuEntity.setSpuCatalogName(inter4SPUVo.getName());//讲解员姓名
        newSpuEntity.setSpuName(inter4SPUVo.getName());//讲解员姓名
        newSpuEntity.setInterpreterId(inter4SPUVo.getInterpreterId());

        SKUEntity newSkuEntity = new SKUEntity();
        newSkuEntity.setId(skuId);
        newSkuEntity.setDiscountPrice(inter4SPUVo.getPrice());
        newSkuEntity.setPrice(inter4SPUVo.getPrice());
        newSkuEntity.setPromotionState(2); //限时折扣
        newSkuEntity.setShopId(inter4SPUVo.getShopId());
        newSkuEntity.setSaleState(1);
        newSkuEntity.setSkuCode(skuCode);
        newSkuEntity.setSpecifications("讲解员");
        newSkuEntity.setSpuName(inter4SPUVo.getName());
        newSkuEntity.setSpuCode(spuCode);
        newSkuEntity.setInterpreterId(inter4SPUVo.getInterpreterId());
        newSkuEntity.setImageUrlOfSKU(inter4SPUVo.getImgUrl());

        spuService.save(newSpuEntity);
        skuService.save(newSkuEntity);
        return true;
    }

    @Override
    public boolean updateByInterId(Inter4SPUVo inter4SPUVo) {
        if(inter4SPUVo == null || StringUtils.isBlank(inter4SPUVo.getInterpreterId())){
            return false;
        }
        SKUEntity skuEntity = skuService.getInterpreterSkuById(inter4SPUVo.getShopId(), inter4SPUVo.getInterpreterId());
        if(skuEntity == null){
            return false;
        }
        SPUEntity spuEntity = spuService.getByShopIdAndSpuCode(inter4SPUVo.getShopId(), skuEntity.getSpuCode());
        if(spuEntity == null){
            return false;
        }

        SKUEntity newSkuEntity = new SKUEntity();
        newSkuEntity.setId(skuEntity.getId());
        newSkuEntity.setImageUrlOfSKU(inter4SPUVo.getImgUrl());
        newSkuEntity.setSpecifications(inter4SPUVo.getName());
        newSkuEntity.setSpuName(inter4SPUVo.getName());
        newSkuEntity.setPrice(inter4SPUVo.getPrice());
        newSkuEntity.setDiscountPrice(inter4SPUVo.getPrice());

        SPUEntity newSpuEntity = new SPUEntity();
        newSpuEntity.setId(spuEntity.getId());
        newSpuEntity.setSpuName(inter4SPUVo.getName());
        newSpuEntity.setSpuCatalogName(inter4SPUVo.getName());//讲解员姓名

        spuService.updateById(newSpuEntity);
        skuService.updateSkuById(newSkuEntity);
        return true;
    }

    @Override
    public boolean deleteByInterIdAndSitesId(String interId, String sitesId) {
        if(StringUtils.isAnyBlank(interId, sitesId)){
            return false;
        }
        SKUEntity skuEntity = skuService.getInterpreterSkuById(sitesId, interId);
        if(skuEntity==null || StringUtils.isBlank(skuEntity.getId())){
            return false;
        }
        boolean deleteSkuFlag = skuService.deleteByInterIdAndShopId(interId, sitesId);
        if(!deleteSkuFlag){
            LOGGER.error("删除讲解员SKU失败。sitesId为：" + sitesId + ",讲解员interId为:" + interId);
            return false;
        }
        boolean delSpuFlag = spuService.deleteByShopIdAndSpuCode(sitesId, skuEntity.getSpuCode());
        if(!delSpuFlag){
            LOGGER.error("删除讲解员SPU失败。sitesId为：" + sitesId + ",讲解员interId为:" + interId);
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Double> getReviewScore4InterByShopId(String shopId) {
        if(StringUtils.isBlank(shopId)){
            return new HashMap<>();
        }
        return spuService.getReviewScore4InterByShopIdAndCataCode(shopId, "1");
    }
}
